﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DPS.Prototype.Provider.Interface;
using DPS.Prototype.Model;
using System.Configuration;
using DPS.DataModel;
using DPS.Prototype.Model.Adapter;

namespace DPS.Prototype.Provider
{
    public class ProductLineProvider:IProductLineProvider
    {
         public List<ProdLine> GetAllProductLine()
        {
            DPSDataModelDataContext dataContext = DataContextProvider.DataContext; List<ProdLine> lines = new List<ProdLine>();
            var query = dataContext.DPSBatchTs.Where(batch => batch.BatchType == "ProdLine" && batch.Status == "1").Select(b => b.BatchID);
            if (query.Count() > 0)
            {
                string batchId = query.First();
                var listLines = dataContext.DPSProdLineByBatchTs.Where(prodLine => prodLine.BatchID == batchId && prodLine.IsActive==1).ToList();
                listLines.ForEach(line =>
                {
                    ProdLine prodLine = ProdlineAdapter.Adapter(line);
                    List<Capacity> capacitys = Factory.CapacityProviderFactory.CreateCapacityProvider().GetCapacityByProdline(prodLine);
                    capacitys = AdaptCapacityList(capacitys);
                    prodLine.Capacitys = capacitys;
                    lines.Add(prodLine);
                });
            }
            return lines;
        }

        private List<Capacity> AdaptCapacityList(List<Capacity> listCapacity)
        {
            List<Capacity> orderedList = listCapacity.OrderBy(c => c.SortNo).ToList();
            int length = orderedList.Count;

            if (length > 1)
            {
                orderedList[0].Previous = null;
                orderedList[0].Next = orderedList[1];
            }
            if (length >= 2)
            {
                orderedList[length - 1].Next = null;
                orderedList[length - 1].Previous = orderedList[length - 2];
            }
            if (length > 2)
            {
                for (int index = 1; index < length - 1; index++)
                {
                    orderedList[index].Previous = orderedList[index - 1];
                    orderedList[index].Next = orderedList[index + 1];
                }
            }
            return orderedList;
        }

        public ProdLine GetProdLineByLoneNo(string lineNo)
        {
            DPSDataModelDataContext dataContext = DataContextProvider.DataContext; 
            ProdLine line = null;
            var query = dataContext.DPSBatchTs.Where(batch => batch.BatchType == "ProdLine" && batch.Status == "1").Select(b => b.BatchID);
            if (query.Count() > 0)
            {
                string batchId = query.First();
                var listLines = dataContext.DPSProdLineByBatchTs.Where(prodLine => prodLine.BatchID == batchId && prodLine.ProdLine==lineNo).ToList();
                if (listLines.Count() == 1)
                {
                    line = ProdlineAdapter.Adapter(listLines[0]);
                    List<Capacity> capacitys = Factory.CapacityProviderFactory.CreateCapacityProvider().GetCapacityByProdline(line);
                    capacitys = AdaptCapacityList(capacitys);
                    line.Capacitys = capacitys;
                }
            }
            return line;
        }
    }
}
